package com.subtlman.logframework.utils;

import com.subtlman.logframework.queue.BuyQueue;
import com.sun.deploy.net.HttpResponse;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @Create: IntelliJ IDEA.
 * @Author: subtlman_ljx
 * @Date: 2022/11/21/10:58
 * @Description: 队列工具类
 */
public class QueueUtil implements Runnable{

    //默认最大值 int MAX_VALUE = 0x7fffffff;
    public static LinkedBlockingQueue<Object> queue = new LinkedBlockingQueue<>();
    private static Thread monitor;


    //存入
    public static void putBody(HttpServletRequest request) throws Exception{
        queue.put(request);
    }

    //处理,多线程处理
    public static String respBody() throws Exception{
        //判断是否空
        //是否需要同步处理
        while (queue != null && queue.size() > 0) {
            System.out.println(queue.size());
            HttpServletRequest request = (HttpServletRequest) queue.take();
            System.out.println(request.getSession());
            Thread.sleep(3000);
            return request.getRequestURL().toString();

        }
        return null;
    }


    // 启动监控线程
    public static String start() {
        monitor = new Thread(() -> {
            while(queue != null && queue.size() > 0) {
                Thread current = Thread.currentThread();
                if(current.isInterrupted()) {
                    System.out.println("料理后事");
                    break;
                }
                try {
                    System.out.println(queue.size());
                    HttpServletRequest request = (HttpServletRequest) queue.take();
                    System.out.println(request.getSession());
                    Thread.sleep(3000);
//                    return request.getRequestURL().toString();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    // 重新设置打断标志
                    current.interrupt();
                }
                System.out.println("线程数："+Thread.activeCount());
            }
        });
//        monitor.start();// 启动线程
        return "线程数："+Thread.activeCount();
    }

    // 停止监控线程
    public void stop() {
        monitor.interrupt();
    }


    @Override
    public void run() {
        while(queue != null && queue.size() > 0) {
            Thread current = Thread.currentThread();
            if(current.isInterrupted()) {
                System.out.println("料理后事");
                break;
            }
            try {
                System.out.println(queue.size());
                HttpServletRequest request = (HttpServletRequest) queue.take();
                System.out.println(request.getSession());
                Thread.sleep(3000);
//                    return request.getRequestURL().toString();
            } catch (InterruptedException e) {
                e.printStackTrace();
                // 重新设置打断标志
                current.interrupt();
            }
            System.out.println("线程数："+Thread.activeCount());
        }
    }
}
